// pages/mine/addAddress/index.js
//var area = require("../addAddress/area.js")
const service = require('../../../service/port.js')
const rules = require('../../../utils/util.js')
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast.js';
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    receiptName: '',
    receiptMobile: '',
    receiptAddress: '',
    checked: true,
    areaList: [],
    show: false,
    address: '',
    nameErrorText: '',
    phoneErrorText: '',
    addressErrorText: '',
    provinceId: '',
    cityId: '',
    areaId: '',
    id:""
  },

  onChange({
    detail
  }) {
    // 需要手动对 checked 状态进行更新
    this.setData({
      checked: detail
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(JSON.parse(options.detail));
    const detail = JSON.parse(options.detail);
    this.setData({
      receiptName: detail.receiptName,
      receiptMobile: detail.receiptMobile,
      receiptAddress: detail.receiptAddress,
      address: detail.provinceName + ' ' + detail.cityName + ' ' + detail.areaName,
      checked: detail.isDefault == '1' ? true : false,
      provinceId: detail.provinceId,
      cityId: detail.cityId,
      areaId: detail.areaId,
      id:detail.id
    })
  },
  onSubmit(data) {

    console.log(data);
    var address = '';
    data.detail.forEach(item => {
      address += item.cname + ' '
    })
    this.setData({
      show: false,
      address: address,
      provinceId: data.detail[0].id,
      cityId: data.detail[1].id,
      areaId: data.detail[2].id
    });
  },
  choose: function() {
    this.setData({
      show: true
    });
  },
  onClose: function() {
    this.setData({
      show: false
    });
  },

  deleteAddress: function() {
    Dialog.confirm({
      title: '提示',
      message: '确认删除该地址吗？',
    })
      .then(() => {
        // on confirm
        service.deleteAddress(this.data.id, res => {
          Toast.success('删除成功');
          wx.redirectTo({
            url: '/pages/mine/address/index',
          })
        })
      })
      .catch(() => {
        // on cancel
      });
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  blurEvent1(e) {
    if (e.detail.value == '') {
      this.setData({
        phoneErrorText: '收件人手机号不能为空！',
      });
    } else {
      this.setData({
        phoneErrorText: '',
        receiptMobile: e.detail.value
      });
    }
  },
  blurEvent(e) {

    if (e.detail.value == '') {
      this.setData({
        nameErrorText: '收件人姓名不能为空！',
      });
    } else {
      this.setData({
        nameErrorText: '',
        receiptName: e.detail.value
      });
    }
  },
  blurEvent2(e) {

    if (e.detail.value == '') {
      this.setData({
        addressErrorText: '详细地址不能为空！',
      });
    } else {
      this.setData({
        addressErrorText: '',
        receiptAddress: e.detail.value
      });
    }
  },
  onChangephone(e) {
    console.log(e.detail)
    if (!rules.regPhone(e.detail)) {
      this.setData({
        phoneErrorText: '收件人手机号格式有误！',
      });
    } else {
      this.setData({
        phoneErrorText: '',
      });
    }
  },
  submit: function(data) {
    if (this.data.receiptName == '') {
      this.setData({
        nameErrorText: '收件人姓名不能为空！',
      });

      return
    };
    if (this.data.receiptMobile == '') {
      this.setData({
        phoneErrorText: '收件人手机号不能为空！',
      });
      return
    };
    if (!rules.regPhone(this.data.receiptMobile)) {
      this.setData({
        phoneErrorText: '收件人手机号格式有误！',
      });
      return
    }
    if (this.data.receiptAddress == '') {
      this.setData({
        addressErrorText: '详细地址不能为空！',
      });

      return
    };
    if (this.data.address == '') {
      Toast.fail('请选择地址');
      return
    };

    service.updateAddrss({
      id: this.data.id, //标识
      customerId: wx.getStorageSync('userInfo').id, //用户标识
      provinceId: this.data.provinceId, //省
      cityId: this.data.cityId, //市
      areaId: this.data.areaId, //区
      receiptName: this.data.receiptName, //收件人姓名
      receiptMobile: this.data.receiptMobile, //收件人手机号
      receiptAddress: this.data.receiptAddress, //收货地址
      isDefault: this.data.checked ? '1' : '0' //设置为默认  1是 0否
    }, res => {
      Toast.success('修改成功');
      wx.navigateBack({
        delta: 1
      })
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})